package com.tju.shortlink.project.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tju.shortlink.project.common.convention.result.Result;
import com.tju.shortlink.project.common.convention.result.Results;
import com.tju.shortlink.project.dto.req.*;
import com.tju.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import com.tju.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.tju.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.tju.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.tju.shortlink.project.handler.CustomBlockHandler;
import com.tju.shortlink.project.service.ILinkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mxl
 * @since 2024-07-06
 */
@Tag(name = "短链接管理相关接口")
@RestController
@RequestMapping("/api/short-link/v1")
@RequiredArgsConstructor
public class LinkController {

    private final ILinkService linkService;

    @Operation(summary = "创建短链接")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    @PostMapping("create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@Validated @RequestBody ShortLinkCreateReqDTO requestParam){
        ShortLinkCreateRespDTO result = linkService.createShortLink(requestParam);
        return Results.success(result);
    }

    @Operation(summary = "修改短链接信息")
    @PutMapping("update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        linkService.updateShortLink(requestParam);
        return Results.success();
    }

    @Operation(summary = "分页查询短链接")
    @GetMapping("page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        return Results.success(linkService.pageShortLink(requestParam));
    }

    @Operation(summary = "分组查询短链接数量")
    @GetMapping("count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("gids") List<String> gids) {
        return Results.success(linkService.listGroupShortLinkCount(gids));
    }

    @Operation(summary = "批量创建短链接")
    @PostMapping("create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(linkService.batchCreateShortLink(requestParam));
    }

    @Operation(summary = "删除指定分组下的短链接")
    @PostMapping("remove")
    public Result<Void> removeLinkByGid(@RequestBody ShortLinkRemoveByGidDTO requestParam){
        linkService.removeLinkByGid(requestParam);
        return Results.success();
    }

}
